$(function () {
    // 查询枚举类
    $.get(baseURL + "logistics/erplogistics/enumlist",
        function(r){
            vm.e.logisticsSendTypeEnumList = r.map.logisticsSendTypeEnumList;
            vm.e.logisticFinanceStatusList = r.map.logisticFinanceStatusList;
            vm.e.merchantList = r.map.merchantList;
        });

    $("#jqGrid").jqGrid({
        url: baseURL + 'logistics/erplogistics/list',
        datatype: "json",
        colModel: [
            { label: '操作', name: 'option', width: 50 , align: 'center',
                formatter:function(value, options, row){
                    var logiNo = row.logiNo == null ? '' : row.logiNo;
                    var logisticsName = row.logisticsName == null ? '' : row.logisticsName;
                    return '<a href="javascript:;" onclick="return vm.getLogisticsInfo(\''+ row.logiId +'\', \''+ logiNo +'\', \''+ logisticsName +'\');">编辑</a>';
                }
            },
            { label: '物流ID', name: 'logiId',  width: 50, key: true ,align:'center'},
            { label: '订单号', name: 'orderId',  width: 80, align:'center'},
            { label: '来源', name: 'sellerName',width: 120 ,align:'center'},
            { label: '发货方式', name: 'sendType',width: 80 ,align:'center',
                formatter: function (value, options, row) {
            		var valueText = value
                    $.each(vm.e.logisticsSendTypeEnumList, function (index, module) {
                        if (module.id == value) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
                }
			},
            { label: '物流名称', name: 'logisticsName',width: 80 ,align:'center'},
            { label: '物流单号', name: 'logiNo',width: 80 ,align:'center'},
            { label: '物流金额', name: 'logiCost',width: 80 ,align:'center'},
            { label: '财务状态', name: 'logisticFinanceStatus',width: 80 ,align:'center',
                formatter: function (value, options, row) {
                    var valueText = value
                    $.each(vm.e.logisticFinanceStatusList, function (index, module) {
                        if (module.id == value) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
                }
             },
            { label: '物流备注', name: 'logiRemark',width: 80 ,align:'center'},
            { label: '发货时间', name: 'sendTime',width: 80 ,align:'center'},
        ],
        viewrecords: true,
        height:  window.innerHeight - 120,
        rowNum: 25,
        rowList : [25,50,100],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.e.logisticsTypeEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').logisticsTypeEnum;
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        logisticsType: {},
        logiInfoVisible: false,
        logiInfo: {
            logiId: '',
            logisticsName: '',
            logiNo: ''
        },
        e:{ //枚举类
            logisticsSendTypeEnumList:[],
            logisticFinanceStatusList:[],
            merchantList:[]
        },
        q:{
            orderNo:null,
            logisticsName:null,
            thirdMerchant:null,
            beginCreatetime:null,
            endCreatetime:null,
            logiNo:null,
            sellerId:null,
            beginLogiCost:null,
            endLogiCost:null,
        },
    },
    methods: {
        query: function () {
            vm.reload();
        },
        getLogisticsInfo: function (logiId, logiNo, logisticsName) {
            vm.logiInfo.logiId = logiId;
            vm.logiInfo.logiNo = logiNo;
            vm.logiInfo.logisticsName = logisticsName;
            vm.logiInfoVisible = true;
        },
        updateLogisticsInfo: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "logistics/erplogistics/updateLogisticsInfo",
                contentType: "application/json",
                data: {
                    'logiId': vm.logiInfo.logiId,
                    'logiNo': vm.logiInfo.logiNo
                },
                success: function(r){
                    if(r.code == 0){
                        vm.logiInfoVisible = false;
                        vm.reload();
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reset:function(){
            vm.q.orderNo= '';
            vm.q.logisticsName= '';
            vm.q.thirdMerchant='';
            vm.q.logiNo='';
            vm.q.beginCreatetime="";
            vm.q.endCreatetime="";
            vm.q.logisticFinanceStatus="";
            vm.q.sellerId="";
            vm.q.beginLogiCost="";
            vm.q.endLogiCost="";
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:{
                    'orderNo':vm.q.orderNo,
                    'logisticsName':vm.q.logisticsName,
                    'thirdMerchant':vm.q.thirdMerchant,
                    'beginCreatetime':vm.q.beginCreatetime,
                    'endCreatetime':vm.q.endCreatetime,
                    'logiNo':vm.q.logiNo,
                    'sellerId':vm.q.sellerId,
                    'beginLogiCost':vm.q.beginLogiCost,
                    'endLogiCost':vm.q.endLogiCost,
                },
            }).trigger("reloadGrid");
        },
        /* 订单起始 */
        checkBeginDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }})
        },
        /* 订单结束 */
        checkEndDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }})
        },
    }
});